################################################################################
#this is a script to batch process species distribution models on the hpc setup

#script was written by Jeremy VanDerWal (jjvanderwal@gmail.com), 27 November 2008

################################################################################
#set some constants

#set the working dir and output dir
working.dir = "/homes/26/jc184963/climate_change2/Rainforest/"
output.dir = "/homes/26/jc184963/climate_change2/models/"
setwd(working.dir)

#species occurrence files
occur.file = 'species.data.csv'
occur = read.csv(occur.file)
#remove any nas
occur = na.omit(occur)

background.folder = '/ctbccr/scratch/Climate/PCMDI/01.EastCoast.250m.75.05/north/mxe/'
################################################################################
#get the unique species
species = levels(occur$SPPCODE)

#cycle through each species
for (spp in species[169:243]) {
  #create a folder for the species
  spp.outfolder = paste(output.dir,spp,"/output/",sep="")
  spp.folder = paste(output.dir,spp,"/",sep="")
  dir.create(spp.outfolder, showWarnings=T, recursive=T)
  #get the occurrances associated with the species
  t.occur = occur[occur$SPPCODE==spp,]
  #write out the occur files file
  write.csv(t.occur,paste(spp.folder,"occur.csv",sep=""),row.names=F)
  #copy over the maxent.jar file
  file.copy("maxent.jar",paste(spp.folder,"maxent.jar",sep=""),overwrite=T) 

  #create the pbs script
  z = file(paste(spp.folder,"PBS_script.pbs",sep=""),"w")
    cat('#!/bin/bash\n',file=z)
    cat('#PBS -c s\n',file=z)
    cat('#PBS -j oe\n',file=z)
    cat('#PBS -m ae\n',file=z)
    cat('#PBS -N maxent_',spp,'\n',sep="",file=z)
    cat('#PBS -M jc184963@jcu.edu.au\n',file=z)
    cat('#PBS -l walltime=9999:00:00\n',file=z)
    cat('#PBS -l nodes=1:ppn=2\n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('echo " This job is allocated 2 cpu on "\n',file=z)
    cat('cat $PBS_NODEFILE\n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('echo "PBS: Submitted to $PBS_QUEUE@$PBS_O_HOST"\n',file=z)
    cat('echo "PBS: Working directory is $PBS_O_WORKDIR"\n',file=z)
    cat('echo "PBS: Job identifier is $PBS_JOBID"\n',file=z)
    cat('echo "PBS: Job name is $PBS_JOBNAME"\n',file=z)
    cat('echo "------------------------------------------------------"\n',file=z)
    cat('cd $PBS_O_WORKDIR\n',file=z)
    cat('\n',file=z)
    cat('#set the source for loading Java later\n',file=z)
    cat('source /etc/profile.d/modules.sh\n',file=z)
    cat('\n',file=z)
    cat('#load the java module\n',file=z)
    cat('module load java-sun\n',file=z)
    cat('\n',file=z)
    cat('#run the model\n',file=z)
    cat('java -mx2500m -jar maxent.jar outputdirectory="',spp.outfolder,'" samplesfile="occur.csv" environmentallayers="',background.folder,'"  -N "bioclim_02" -N "bioclim_03" -N "bioclim_06" -N "bioclim_07" -N "bioclim_08" -N "bioclim_09" -N "bioclim_10" -N "bioclim_11" -N "bioclim_13" -N "bioclim_16" -N "bioclim_17" -N "bioclim_18" -N "bioclim_19" nothreshold nowarnings noaskoverwrite responsecurves jackknife novisible nooutputgrids autorun\n',sep="",file=z)
    cat('\n',file=z)
  close(z)
    
  #change the working directory and submit the pbs script
  setwd(spp.folder)
  system("qsub PBS_script.pbs")
  system("sleep 2")
  #reset the working directory
  setwd(working.dir)
}



